package cn.edu.jnu.experiment1.service.impl;

import cn.edu.jnu.experiment1.common.Page;
import cn.edu.jnu.experiment1.entity.User;
import cn.edu.jnu.experiment1.mapper.UserMapper;
import cn.edu.jnu.experiment1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findByOrder() {
        return userMapper.findByOrder();
    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public List<User> findByUsername(String name) {
        return userMapper.findByUsername(name);
    }

    @Override
    public List<User> findByMore(String username, String name) {
        return userMapper.findByMore(username, name);
    }

    @Override
    public List<User> findByLike(String username, String name) {
        return userMapper.findByLike(username, name);
    }

    @Override
    public Page<User> findByPage(Integer pageNum, Integer pageSize, String username, String name) {
        Page<User> Page = new Page<>();
        Integer skipNum = (pageNum - 1) * pageSize;
        List<User> userList = userMapper.findByPage(skipNum, pageSize, username, name);
        int total = userMapper.selectCount(username, name);
        Page.setList(userList);
        Page.setTotal(total);
        return Page;
    }

    @Override
    public int insert(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public int update(User user) {
        return userMapper.update(user);
    }

    @Override
    public void delete(Integer id) {
        userMapper.delete(id);
    }

    @Override
    public void deleteAll(List<Integer> ids) {
        for(Integer id : ids) {
            userMapper.delete(id);
        }
    }
}
